(library (array-helpers)
  (export array-len-in-dim
          arrays->hash-table)
  (import
   (except (rnrs base) let-values map error)
   (only (guile)
         lambda* λ
         current-output-port
         ;; arrays
         array-shape
         array-ref)
   ;; lists
   (srfi srfi-1)
   ;; hash tables
   (srfi srfi-69))


  (define array-len-in-dim
    (λ (arr dim)
      (let* ([shape (array-shape arr)]
             [dim-min-max (list-ref shape dim)])
        (+ (- (second dim-min-max)
              (first dim-min-max))
           1))))

  (define arrays->hash-table
    (lambda* (keys-arr vals-arrs #:optional (equal-func equal?))
      (let ([rows (array-len-in-dim keys-arr 0)]
            [cols (array-len-in-dim keys-arr 1)]
            [table (make-hash-table equal-func)])
        (let iter-rows ([row-ind 0])
          (let iter-cols ([col-ind 0])
            (cond
             [(< row-ind rows)
              (cond
               [(< col-ind cols)
                (hash-table-set! table
                                 (array-ref keys-arr row-ind col-ind)
                                 (array-ref vals-arrs row-ind col-ind))
                (iter-cols (+ col-ind 1))]
               [else (iter-rows (+ row-ind 1))])]
             [else table])))))))
